<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MlcmsUserWithdrawlog extends Model
{
    protected $table="mlmcms_user_drawallog";
    public $timestamps=false;
    protected $fillable = [];
    protected $guarded =[];
    protected $primaryKey = "MUDID";
    
    /**
     * 添加提现
     * @param \Illuminate\Http\Request $request
     */
    public static function addWithLog(\Illuminate\Http\Request $request) {
        $user = $request->session()->get("Home");
        $sets = MlcmsSetmat::getSets();
        if ($request->input('Mud_AddMoney') < 100) {
            return aaa_return(0, "提现金额要大于100才可以提现");
        }
        if ($user->Mu_AllMoney < $request->input('Mud_AddMoney')) {
            return aaa_return(0, "操作非法，提现金币不足");
        }
        //添加提现记录
        $data['Mud_Number'] = $user->Mu_Number;
        $data['Mud_OpenBankName'] = $user->Mu_OpenBankName;
        $data['Mud_OpenBankNo'] = $user->Mu_OpenBankNo;
        $data['Mud_AddMoney'] = $request->input('Mud_AddMoney');
        $data['Mud_OldMoney'] = $user->Mu_AllMoney;
        $data['Mud_Money'] = $data['Mud_OldMoney'] - $data['Mud_AddMoney'];
        $data['Mud_FixNumber'] = $user->Mu_Number;
        $data['Mud_IsApproved'] = 0;
        $data['Mud_TaxMoney'] = $data['Mud_AddMoney'] * $sets->Mat_withTax;
        $data['Mud_ApprovedTime'] = null;
        $data['Mud_Type'] = 1;
        $data['Mud_CreateTime'] = date("Y-m-d H:i:s");
        $data['Mud_Deo'] = $request->input('Mud_Deo');
        if (static::insert($data)) {
            //更新用户账户
            MlcmsUser::where('MUID', $user->MUID)->update(['Mu_AllMoney'=>$data['Mud_Money']]);
            $user->Mu_AllMoney = $data['Mud_Money'];
            return aaa_return(1, "申请提交成功，我们会尽快审核处理!!");
        } 
        return aaa_return(0, "申请提交失败");
    }
    
    /**
     * 提现类型
     * @return string
     */
    public function withdrawType() {
        switch ($this->Mud_Type) {
            case 1:
                return "奖金币提现";
        }
    }
    
    /**
     * 审核状态
     */
    public function isApproved() {
        return $this->Mud_IsApproved ? "<span class='text-success'>已审核</span>":"<span class='text-danger'>未审核</span>";
    }
    /**
     * 实发金额
     */
    public function sfMoney() {
        return sprintf("%.4f",$this->Mud_AddMoney - $this->Mud_TaxMoney);
    }
}
